package com.pxmeta.app.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.constant.UserVisitMode;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.user.entity.UserVisit;
import com.pxmeta.user.entity.vo.UserVisitVO;
import com.pxmeta.user.entity.vo.VisitResultVO;
import com.pxmeta.user.service.IUserVisitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user/visit")
@RequiredArgsConstructor
@Api(value = "我的访客", tags = "我的访客")
public class UserVisitController {
    private final IUserVisitService visitService;

    @SaCheckLogin
    @ApiOperation("谁看过我")
    @GetMapping("/sawMe")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "page", value = "页码"),
                    @ApiImplicitParam(name = "size", value = "每页记录数")
            })
    public AjaxResult<VisitResultVO> sawMe(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "20") Integer size) {
        Long userId = SecurityUtils.getUserId();
        VisitResultVO data = visitService.getSawMeData(userId, page, size);
        return AjaxResult.success(data);
    }

    @SaCheckLogin
    @ApiOperation("我看过谁")
    @GetMapping("/sawWho")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "page", value = "页码"),
                    @ApiImplicitParam(name = "size", value = "每页记录数")
            })
    public AjaxResult<List<UserVisitVO>> SawWho(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "20") Integer size) {
        Long userId = SecurityUtils.getUserId();
        List<UserVisitVO> data = visitService.getSawWhoData(userId, page, size);
        return AjaxResult.success(data);
    }

    @SaCheckLogin
    @ApiOperation("增加一条访客记录")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "targetUserId", value = "目标用户userId"),
                    @ApiImplicitParam(name = "visitMode", value = "访问模式：0主页，1聊天框")
            })
    @GetMapping("/insertUserVisit")
    public AjaxResult insertUserVisit(@RequestParam(required = true) Long targetUserId,
                                      @RequestParam(required = true) Integer visitMode) {
        Long userId = SecurityUtils.getUserId();
        if (UserVisitMode.HOME != visitMode && UserVisitMode.CHAT != visitMode) {
            return AjaxResult.error(HttpStatus.PARAMS_ERROR, "不支持的访问模式！");
        }
        if (!targetUserId.equals(userId)){
            visitService.save(UserVisit.builder().userId(userId).targetUserId(targetUserId).visitMode(visitMode).visitDate(LocalDateTime.now()).build());
        }
        return AjaxResult.success();
    }
}
